﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T.ApiResults;
using T.Personnel.Domain.Dto;
using T.Personnel.Domain;
using T.Personnel.Read.API.Application.Command.FullManageCommand;
using T.Personnel.Read.API.Application.Command.ResignationManageCommand;
using Microsoft.EntityFrameworkCore;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using Microsoft.AspNetCore.Authorization;

namespace T.Personnel.Read.API.Controllers
{
    /// <summary>
    /// 人事管理-离职办理-读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ReadResignationManageController : ControllerBase
    {
        private readonly IMediator mediator;

        public ReadResignationManageController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取离职列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Result<Manage>>> GetListResignationManageAsync([FromQuery] GetListResignationManageCommand command)
        {
            ApiResult<Result<Manage>> result = new ApiResult<Result<Manage>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 获取离职反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Manage>> GetResignationManageAsync([FromQuery] GetResignationManageCommand command)
        {
            ApiResult<Manage> result = new ApiResult<Manage>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 一年每月离职人数统计
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<MonthlyLeaveCount>> GetAllResignationAsync([FromQuery] GetAllResignationCommand command)
        {
            var year = DateTime.Now.Year;
            var gather = await mediator.Send(command);
            gather = gather.Where(x=>x.AppStatus=="已同意").ToList();
            // 获取指定年份的员工离职数据，按月份分组并统计每月的离职人数
            var leaveCounts = gather
                .Where(m => m.LastDay.HasValue && m.LastDay.Value.Year == year)
                .GroupBy(m => m.LastDay.Value.Month) // 按月分组
                .Select(g => new
                {
                    Month = g.Key,
                    Count = g.Count()
                })
                .ToList();

            // 使用 1 到 12 月份的范围，确保没有离职员工的月份也显示为 0
            var result = Enumerable.Range(1, 12)
                .Select(month => new MonthlyLeaveCount
                {
                    Month = month,
                    LeaveCount = leaveCounts.FirstOrDefault(l => l.Month == month)?.Count ?? 0
                })
                .ToList();

            return result;
        }

        /// <summary>
        /// 当前月离职人数
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<int> GetLeavingCountForCurrentMonthAsync([FromQuery] GetAllResignationCommand command)
        {
            var gather = await mediator.Send(command);
            gather = gather.Where(x => x.AppStatus == "已同意").ToList();
            // 获取当前月份
            var currentMonth = DateTime.Now.Month;
            var currentYear = DateTime.Now.Year;

            // 查询离职员工数量
            var leaveCount = gather
                .Where(m => m.LastDay.HasValue &&
                            m.LastDay.Value.Year == currentYear &&
                            m.LastDay.Value.Month == currentMonth)
                .Count();

            return leaveCount;
        }

        /// <summary>
        /// 获取审批离职列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Result<Manage>>> GetListResignationsManageAsync([FromQuery] GetListResignationsManageCommand command)
        {
            ApiResult<Result<Manage>> result = new ApiResult<Result<Manage>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
